package cn.zt.service.impl;

import cn.zt.common.Result;
import cn.zt.domain.Balance;
import cn.zt.domain.BankCardType;
import cn.zt.domain.Bankcard;
import cn.zt.domain.vo.AccountInformationVo;
import cn.zt.feign.BalanceFeign;
import cn.zt.feign.BankCardTypeFeign;
import cn.zt.feign.BankcardFeign;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zt.domain.AccountInformation;
import cn.zt.service.AccountInformationService;
import cn.zt.mapper.AccountInformationMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author asus
* @description 针对表【account_information(开户信息表)】的数据库操作Service实现
* @createDate 2024-09-03 18:43:32
*/
@Service
public class AccountInformationServiceImpl extends ServiceImpl<AccountInformationMapper, AccountInformation>
    implements AccountInformationService{

    @Resource
    private BalanceFeign balanceFeign;
//    @Resource
//    private BankcardFeign bankcardFeign;
//    @Resource
//    private BankCardTypeFeign bankCardTypeFeign;

    @Override
    public Result kaihu(AccountInformationVo accountInformationVo) {
        System.out.println(accountInformationVo);
        AccountInformation accountInformation = new AccountInformation();
        accountInformation.setUsername(accountInformationVo.getUsername());
        accountInformation.setECard(accountInformationVo.getYhkh());
        accountInformation.setCreditScore(100);
        accountInformation.setCreateTime(new Date());
        accountInformation.setUserId(accountInformationVo.getUserId());
        boolean save = this.save(accountInformation);
            //添加银行卡
            Bankcard bankcard = new Bankcard();
            bankcard.setBankCard(accountInformationVo.getYhkh());
            bankcard.setNickname(accountInformationVo.getUsername());
            bankcard.setPhone(accountInformationVo.getPhone());
            bankcard.setPayPassword(accountInformationVo.getPassword());
        int i = balanceFeign.addBankcard(bankcard);
        //关联余额
            Balance balance = new Balance();
        int i1 = balanceFeign.addBalance(balance);
        //银行卡类型
            BankCardType bankCardType = new BankCardType();
            bankCardType.setAccountId(accountInformation.getId());
            bankCardType.setBankcardId(i);
            bankCardType.setBankTypeId(1);
            bankCardType.setBalanceId(i1);
            bankCardType.setAccountCardTypeId(accountInformationVo.getCardType());
            balanceFeign.addBankcardType(bankCardType);
        return save?Result.success("开户成功"):Result.error("开户失败");
    }

    @Override
    public Result bangka(Integer id) {
        AccountInformation accountInformation = new AccountInformation();
        accountInformation.setId(id);
        accountInformation.setIsBankcard(2);
        boolean b = this.updateById(accountInformation);
        return b?Result.success("绑卡成功"):Result.error("绑卡失败");
    }

    @Override
    public Result selectById(String eCard) {
        AccountInformation one = this.getOne(Wrappers.<AccountInformation>lambdaQuery().eq(AccountInformation::getECard, eCard));
        return Result.success(one);
    }
}




